/**
 * @Title: NetworkStatusReceiver.java
 * @Package com.sinoglobal.wallet.receiver
 * @Description: TODO(用一句话描述该文件做什么)
 * @author hpq
 * @date 2015-9-11 上午9:28:14
 */
	
package com.sinoglobal.wallet.receiver;

import com.sinoglobal.wallet.receiver.W_NetworkStatusReceiver;
import com.sinoglobal.wallet.app.SinoApplication;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.TelephonyManager;
import android.widget.Toast;

/**
 * @author hpq
 * @createdate 2015-9-11 上午9:28:14
 * @Description: 网络监控
 */
public class W_NetworkStatusReceiver extends BroadcastReceiver {

	private static final String TAG = W_NetworkStatusReceiver.class.getSimpleName();
	private static final int TDS_HSDPA = 18;//3g
	private static final int TD_SCDMA = 17;//移动3g

	@Override
	public void onReceive(Context context, Intent intent) {
		String action = intent.getAction();
		if(action.equals(ConnectivityManager.CONNECTIVITY_ACTION)){			
			ConnectivityManager connectivity = (ConnectivityManager)context
					.getSystemService(Context.CONNECTIVITY_SERVICE);
			NetworkInfo info = connectivity.getActiveNetworkInfo();
//			Log.d(TAG, "network:"+info);
			if(info!=null && info.isAvailable()){
				SinoApplication.getInstance().setNetStatus(true);
				if(info.isConnected()){
					confirmNetType(info);
				}
			}else{
				System.out.println("请检查网络是否连接！");
				SinoApplication.getInstance().setNetStatus(false);
				Toast.makeText(context, "请检查网络是否连接！", Toast.LENGTH_SHORT).show();
			}
		}
	}

	private void confirmNetType(NetworkInfo info) {
		int type = info.getType();
		switch(type){
		case ConnectivityManager.TYPE_MOBILE:
			int subType = info.getSubtype();
			switch(subType){
			case TelephonyManager.NETWORK_TYPE_UNKNOWN:
				SinoApplication.getInstance().setNetType("2G");
				break;
			case TelephonyManager.NETWORK_TYPE_GPRS://  ~ 100 kbps
			case TelephonyManager.NETWORK_TYPE_EDGE://移动2g ~ 50-100 kbps
			case TelephonyManager.NETWORK_TYPE_CDMA://电信2g ~ 14-64 kbps
			case TelephonyManager.NETWORK_TYPE_1xRTT:// ~ 50-100 kbps
			case TelephonyManager.NETWORK_TYPE_IDEN:// ~25 kbps 
				SinoApplication.getInstance().setNetType("2G");
				break;
			case TelephonyManager.NETWORK_TYPE_UMTS://联通3g ~ 400-7000 kbps
			case TelephonyManager.NETWORK_TYPE_EVDO_0://版本0.（电信3g） ~ 400-1000 kbps
			case TelephonyManager.NETWORK_TYPE_EVDO_A://版本A （电信3g） ~ 600-1400 kbps
			case TelephonyManager.NETWORK_TYPE_HSDPA://联通3g ~ 2-14 Mbps
			case TelephonyManager.NETWORK_TYPE_HSUPA:// ~ 1-23 Mbps
			case TelephonyManager.NETWORK_TYPE_HSPA:// ~ 700-1700 kbps
			case TelephonyManager.NETWORK_TYPE_EVDO_B://版本B（电信3g） ~ 5 Mbps
			case TelephonyManager.NETWORK_TYPE_LTE://3g到4g的一个过渡，称为准4g ~ 10+ Mbps
			case TelephonyManager.NETWORK_TYPE_EHRPD:// ~ 1-2 Mbps
			case TelephonyManager.NETWORK_TYPE_HSPAP://// ~ 10-20 Mbps
				SinoApplication.getInstance().setNetType("3G/4G");
				break;
			case TD_SCDMA:
			case TDS_HSDPA:
				SinoApplication.getInstance().setNetType("3G/4G");
				break;
			}
			break;
		case ConnectivityManager.TYPE_WIFI:
			SinoApplication.getInstance().setNetType("wifi");
			break;
	
		}
	}

}
